﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.IO;

/// <summary>
/// Summary description for KhacHang
/// </summary>
public class KH_Admin
{

	public KH_Admin()
	{
		//
		// TODO: Add constructor logic here
		//
    }
    DataConnection data = new DataConnection();
    public DataTable LayDSKH()
    {
        string sql = "select * from KhachHang";
        return data.GetData(sql);
    }
    public DataTable LayDSKHTheoMa(String ma)
    {
        SqlParameter p_ma = new SqlParameter("@Makh", ma);
        string sql = "select MaKH from KhachHang";
        return data.GetData(sql, p_ma);
    }
    public DataTable Sua_khachang(int makh,string hoten, string tendn, string matkhau, bool gioitinh, string ngaysinh, string diachi, string email, string dienthoai)
    {
        SqlParameter pma = new SqlParameter("@ma", makh);
        SqlParameter pho = new SqlParameter("@hoten", hoten);
        SqlParameter pTDN = new SqlParameter("@tenDN", tendn);
        SqlParameter pMK = new SqlParameter("@matkhau",matkhau);
        SqlParameter pgioitinh = new SqlParameter("@gioitinh", gioitinh);
        SqlParameter pngaysinh = new SqlParameter("@ngaysinh", ngaysinh);
        SqlParameter pdiachi = new SqlParameter("@diachi", diachi);
        SqlParameter pEmail = new SqlParameter("@email",email);
        SqlParameter pdienthoai = new SqlParameter("@dienthoai", dienthoai);
        string sql = "update KhachHang set HoTen=@hoten,TenDN=@tenDN,MatKhau=@matKhau,GioiTinh=@gioitinh,NgaySinh=@ngaysinh,DiaChi=@diachi,Email=@email,DienThoai=@dienthoai where MaKH=@ma";
        return data.GetData(sql, pma, pho, pTDN, pMK, pgioitinh, pngaysinh, pdiachi,pEmail,pdienthoai );
    }

    public DataTable InsertKhachHang(string hoten, string tendn, string matkhau, bool gioitinh, string ngaysinh, string diachi, string email, string dienthoai)
    {
        string sql = "insert into KhachHang (HoTen,TenDN,MatKhau,GioiTinh,NgaySinh,DiaChi,DienThoai,Email) values (@HoTen,@TenDN,@MatKhau,@GioiTinh,@NgaySinh,@DiaChi,@Email,@DienThoai)";
        SqlParameter p1 = new SqlParameter("@hoten", hoten);
        SqlParameter p8 = new SqlParameter("@TenDN",tendn);
        SqlParameter p9 = new SqlParameter("@MatKhau", matkhau);
        SqlParameter p2 = new SqlParameter("@GioiTinh", gioitinh);
        SqlParameter p3 = new SqlParameter("@NgaySinh", ngaysinh);
        SqlParameter p4 = new SqlParameter("@DiaChi", diachi);
        SqlParameter p6 = new SqlParameter("@Email", email);
        SqlParameter p5 = new SqlParameter("@DienThoai", dienthoai);
        return data.GetData(sql, p1,p8,p9, p2,p3,p4,p6,p5);
    }
    public DataTable XoaKH(int ma)
    {
        string sql = "delete KhachHang where MaKH=@MaKh";
        SqlParameter maKH = new SqlParameter("@MaKh",ma);
        return data.GetData(sql,maKH);
    }

    public DataTable TimKH(string ten,bool gioitinh)
    {

        string sql = "select MaKH,HoTen,TenDN,MatKhau,GioiTinh,NgaySinh,DiaChi,DienThoai,Email from KhachHang where 1=1";
        if (ten != null)
            sql = sql + " and HoTen like '%'+@ten+'%'";
        if (gioitinh != null)
            sql = sql + " and GioiTinh=@gioitinh";
        SqlParameter p = new SqlParameter("@ten", ten);
        SqlParameter p1 = new SqlParameter("@gioitinh", gioitinh);
        return data.GetData(sql, p,p1);
   }
//lay thong tinh ca nhan
    public DataTable TTKH(string sua)
    {
        string sql = "select * from KhachHang where MaKH=@sua";
        SqlParameter p = new SqlParameter("@sua", sua);
        return data.GetData(sql,p);
    }
    public string DSTTKH(string Ten, string pass)
    {
        string sql = "select TenDN,MatKhau from KhachHang where TenDN=@Ten and MatKhau=@pass";
        SqlParameter u = new SqlParameter("@TenDN", Ten);
        SqlParameter p = new SqlParameter("@MatKhau", pass);
        DataTable dt = data.GetData(sql, u, p);
        if (dt.Rows.Count > 0)
            return dt.Rows[0][0].ToString();
        else
            return "";

    }
   
}
